package com.mrjy.share.bo;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.DistributorInfo;
import com.mrjy.common.entity.DistributorTeamInfo;
import com.mrjy.common.entity.ServiceStationInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.common.util.MD5Encrypt;
import com.mrjy.common.util.UpdateDisableFlagUtil;
import com.mrjy.maintain.dao.IPartnerInfoDao;
import com.mrjy.maintain.dao.IServiceStationInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.dto.PartnerInfoDTO;
import com.mrjy.maintain.util.RoleInfoEnum;
import com.mrjy.share.dao.IDistributorInfoDao;
import com.mrjy.share.dao.IDistributorTeamInfoDao;
import com.mrjy.share.dto.DistributorInfoDTO;
import com.mrjy.share.dto.DistributorTeamInfoDTO;
import com.mrjy.share.dto.PromoterDTO;

@Service("distributorInfoBo")
public class DistributorInfoBo implements IDistributorInfoBo{
	
	@Resource
	private IDistributorInfoDao distributorInfoDao;
	
	@Resource
	private IDistributorTeamInfoDao distributorTeamInfoDao;
	
	@Resource
	private IPartnerInfoDao partnerInfoDao;
	
	@Resource
	private IServiceStationInfoDao serviceStationInfoDao;
	
	@Resource
	private IUserInfoDao userInfoDao;
	
	@Override
	public Object queryDistributorInfoPage(DistributorInfoDTO dto,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			map.put("err_code", -1);
			map.put("err_msg", "当前没有登录或登录超时");
			return map;
		}
	/*	if(userInfo.getRoleId() != 1){
			map.put("err_code", -2);
			map.put("err_msg", "只有管理员才能查看");
			return map;
		}*/
		int count = distributorInfoDao.queryDistributorInfoCount(dto);
		DistributorInfoDTO vo = new DistributorInfoDTO();
		//全站推广者
//		int totalPromoterNum = distributorInfoDao.queryDistributorInfoCount(vo);
        if(count > 0){
			List<DistributorInfoDTO> list = distributorInfoDao.queryDistributorInfoPage(dto);
			if(null != list && list.size() > 0){
				map.put("err_code", 0);
				map.put("err_msg", "查询成功");
				map.put("rows", list);
				map.put("total", count);
                map.put("totalPromoterNum", count);
            }else {
				map.put("err_code", 1);
				map.put("err_msg", "查询成功,无数据");
				map.put("rows", new ArrayList<DistributorInfoDTO>());
				map.put("total", 0);
                map.put("totalPromoterNum", count);
            }
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询成功,无数据");
			map.put("rows", new ArrayList<DistributorInfoDTO>());
			map.put("total", 0);
            map.put("totalPromoterNum", count);
        }
		
		return map;	
	}

	@Override
	public Object txAdd(DistributorInfo info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if(StringUtils.isBlank(info.getDistributorName())){
			map.put("err_code", 400);
			map.put("err_msg", "分销商名称不能为空");
			return map;
		}
		if(info.getDistributorName().length()>16){
			map.put("err_code", 401);
			map.put("err_msg", "分销商名称不能大于16个字符");
			return map;
		}
		if(StringUtils.isBlank(info.getMobilePhone())){
			map.put("err_code", 402);
			map.put("err_msg", "手机号不能为空");
			return map;
		}
		
		/*if (StringUtils.isBlank(info.getBankCard())) {
			map.put("err_code", 403);
			map.put("err_msg", "银行卡号不能为空");
			return map;
		}
		if (StringUtils.isNotBlank(info.getBankCard()) && 
				!RegexUtil.checkICBC(info.getBankCard())) {
			map.put("err_code", 404);
			map.put("err_msg", "银行卡格式不正确,请填写正确的工商银行卡号");
			return map;
		}*/
		if (StringUtils.isNotBlank(info.getAddress()) && 
				info.getAddress().length()>30) {
			map.put("err_code", 405);
			map.put("err_msg", "详细地址不能超过30个字符");
			return map;
		}
		if (StringUtils.isBlank(info.getDistributorType())) {
			map.put("err_code", 406);
			map.put("err_msg", "分销商类型不能为空");
			return map;
		}
		if (info.getSharingProportion() == null) {
			map.put("err_code", 407);
			map.put("err_msg", "分成比例不能为空");
			return map;
		}
		
		UserInfo userInfo = userInfoDao.queryByMobilePhone(info.getMobilePhone());
		int count = 0;
		if(userInfo != null  ){
			if(userInfo.getRoleId() != RoleInfoEnum.STUDENT.getRoleId() 
					&& userInfo.getRoleId() != RoleInfoEnum.TEACHER.getRoleId()
					&& userInfo.getRoleId() != RoleInfoEnum.SCHOOL_ADMIN.getRoleId()){//不是普通用户、讲师和学校
				map.put("err_code", 203);
				map.put("err_msg", "该手机已经被其他角色（非普通用户、讲师和学校）使用，请换个手机号码试试");
				return map;
			}else {
				if(StringUtils.isBlank(userInfo.getRecommendCode())){
					userInfo.setRecommendCode(userInfoDao.generateRecommendCodeOne(8));
				}
				if(userInfo.getRoleId() == RoleInfoEnum.STUDENT.getRoleId() 
						|| userInfo.getRoleId() == RoleInfoEnum.TEACHER.getRoleId()){
					userInfo.setRoleId(RoleInfoEnum.DISTRIBUTOR_ADMIN.getRoleId());
				}else if(userInfo.getRoleId() != RoleInfoEnum.SCHOOL_ADMIN.getRoleId()){
					userInfo.setRoleId(RoleInfoEnum.DISTRIBUTOR_SCHOOL_ADMIN.getRoleId());
				}
				userInfo.setMobilePhone(info.getMobilePhone());
				userInfo.setIdCard(info.getIdCard());
				userInfo.setProvince(info.getProvince());
				userInfo.setCity(info.getCity());
				userInfo.setCounty(info.getTown());
				if(userInfo.getPassword() != null){
					userInfo.setPassword(MD5Encrypt.encryptByMD5("123abc"));
				}
				//银行卡
	            userInfo.setBankCardOwner(info.getDistributorName());
	            userInfo.setBankCard(info.getBankCard());
	            
				count = userInfoDao.updateById(userInfo);
			}
		}else {
			userInfo = new UserInfo();
			userInfo.setRoleId(RoleInfoEnum.DISTRIBUTOR_ADMIN.getRoleId());//分销商
			userInfo.setUserName(info.getDistributorName());
			userInfo.setRecommendCode(userInfoDao.generateRecommendCodeOne(8));
			userInfo.setMobilePhone(info.getMobilePhone());
			userInfo.setIdCard(info.getIdCard());
			userInfo.setProvince(info.getProvince());
			userInfo.setCity(info.getCity());
			userInfo.setCounty(info.getTown());
			userInfo.setStreet(info.getAddress());
			userInfo.setPassword(MD5Encrypt.encryptByMD5("123abc"));
			userInfo.setRegisterDate(new Date().getTime());
			userInfo.setDisableFlag(0);
			//银行卡
            userInfo.setBankCardOwner(info.getDistributorName());
            userInfo.setBankCard(info.getBankCard());
			count = userInfoDao.insert(userInfo);
		}
		
		//插入分类标签记录
		if (count == 1) {
			//新增一条为分销商身份的推广者记录
			DistributorInfo dInfo = new DistributorInfo();
			dInfo.setUserId(userInfo.getId());
			dInfo.setDistributorName(info.getDistributorName());
			dInfo.setMobilePhone(info.getMobilePhone());
			dInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
			dInfo.setDeadlineTime(new Timestamp(DateUtil.dateTimeAddYears(System.currentTimeMillis(), 1)));
			dInfo.setIsUpgrading("1");
			dInfo.setUpgradeTime(new Timestamp(System.currentTimeMillis()));
			dInfo.setShareCode(userInfo.getRecommendCode());
			dInfo.setIdCard(info.getIdCard());
			dInfo.setBankCard(info.getBankCard());
			dInfo.setDistributorType(info.getDistributorType());
			//自定义分成比例
			dInfo.setSharingProportion(info.getSharingProportion());
			dInfo.setProvince(info.getProvince());
			dInfo.setCity(info.getCity());
			dInfo.setTown(info.getTown());
			dInfo.setAddress(info.getAddress());
			dInfo.setPostalStatus("0");
			dInfo.setDisabledFlag(0);

			distributorInfoDao.insert(dInfo);
            

			map.put("err_code", 200);
			map.put("err_msg", "添加成功");
		}else{
			map.put("err_code", 500);
			map.put("err_msg", "添加失败");
		}	
		return map;
		
	}

	@Override
	public Object txEdit(DistributorInfo info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if(StringUtils.isBlank(info.getDistributorName())){
			map.put("err_code", 400);
			map.put("err_msg", "分销商名称不能为空");
			return map;
		}
		if(info.getDistributorName().length()>16){
			map.put("err_code", 401);
			map.put("err_msg", "分销商名称不能大于16个字符");
			return map;
		}
		if(StringUtils.isBlank(info.getMobilePhone())){
			map.put("err_code", 402);
			map.put("err_msg", "手机号不能为空");
			return map;
		}
		
		/*if (StringUtils.isBlank(info.getBankCard())) {
			map.put("err_code", 403);
			map.put("err_msg", "银行卡号不能为空");
			return map;
		}
		if (StringUtils.isNotBlank(info.getBankCard()) && 
				!RegexUtil.checkICBC(info.getBankCard())) {
			map.put("err_code", 404);
			map.put("err_msg", "银行卡格式不正确,请填写正确的工商银行卡号");
			return map;
		}*/
		if (StringUtils.isNotBlank(info.getAddress()) && 
				info.getAddress().length()>30) {
			map.put("err_code", 405);
			map.put("err_msg", "详细地址不能超过30个字符");
			return map;
		}
		if (StringUtils.isBlank(info.getDistributorType())) {
			map.put("err_code", 406);
			map.put("err_msg", "分销商类型不能为空");
			return map;
		}
		if (info.getSharingProportion() == null) {
			map.put("err_code", 407);
			map.put("err_msg", "分成比例不能为空");
			return map;
		}
		int count = distributorInfoDao.updateById(info);
		
		if(count == 1){
			map.put("err_code", 200);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "更新失败");
		}
		return map;
	}

	@Override
	public Object queryById(DistributorInfo info) {
		Map<String,Object> map = new HashMap<String,Object>();
		DistributorInfoDTO dto = distributorInfoDao.queryById(info.getId());
		if(dto != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", dto);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询失败");
			map.put("info", dto);
		}
		return map;
	}

	@Override
	public Object queryPageByDistributorType(DistributorInfoDTO dto,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			map.put("err_code", -1);
			map.put("err_msg", "当前没有登录或登录超时");
			return map;
		}
		/*if(userInfo.getRoleId() != 1){
			map.put("err_code", -2);
			map.put("err_msg", "只有管理员才能查看");
			return map;
		}*/
		DistributorInfoDTO totalDTO = distributorInfoDao.queryByDistributorTypeTotalNum(dto);
		if(totalDTO  != null && totalDTO.getTotalDistributorNum() > 0){
			List<DistributorInfoDTO> list = distributorInfoDao.queryPageByDistributorType(dto);
			if(null != list && list.size() > 0){
				map.put("err_code", 0);
				map.put("err_msg", "查询成功");
				map.put("rows", list);
				map.put("total", distributorInfoDao.queryCountByDistributorType(dto));
				map.put("totalDistributorNum", totalDTO.getTotalDistributorNum());
				map.put("totalMiddleDistributorNum", totalDTO.getTotalMiddleDistributorNum());
				map.put("totalHighDistributorNum", totalDTO.getTotalHighDistributorNum());
			}else {
				map.put("err_code", 1);
				map.put("err_msg", "查询成功,无数据");
				map.put("rows", new ArrayList<DistributorInfoDTO>());
				map.put("total", 0);
				map.put("totalDistributorNum", 0);
				map.put("totalMiddleDistributorNum", 0);
				map.put("totalHighDistributorNum", 0);
			}
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询成功,无数据");
			map.put("rows", new ArrayList<DistributorInfoDTO>());
			map.put("total", 0);
			map.put("totalDistributorNum", 0);
			map.put("totalMiddleDistributorNum", 0);
			map.put("totalHighDistributorNum", 0);
		}
		
		return map;	
	}

	@Override
	public Object txUpdatePostalStatus(DistributorInfo info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		int count = distributorInfoDao.updatePostalStatus(info);
		if(count > 0){
			map.put("err_code", 200);
			map.put("err_msg", "更新成功");
			map.put("count", count);
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "更新失败");
			map.put("count", count);
		}
		return map;
	}

	@Override
	public Object txUpgradeToDistributor(DistributorInfo info) {
		Map<String,Object> map = new HashMap<String,Object>();
		DistributorInfo updateInfo = distributorInfoDao.queryById(info.getId());
		int count = 0;
		if(updateInfo != null){
			//查询是否有正在升级中的数据 (isUpgrating=0)
			DistributorInfoDTO tempDTO = distributorInfoDao.queryIsUpgradingByUserId(updateInfo.getUserId());
			if(tempDTO != null){
				//正在升级中,则更新之前的数据
				tempDTO.setDistributorType("0");//暂时设置成"0",普通推广者，待成功支付后回写状态
				tempDTO.setCreateTime(new Timestamp(System.currentTimeMillis()));
				tempDTO.setAddress(info.getAddress());
				tempDTO.setFuCardNum(info.getFuCardNum());
				tempDTO.setBankCard(info.getBankCard());
				tempDTO.setIdCard(info.getIdCard());
				tempDTO.setMobilePhone(info.getMobilePhone());
				count = distributorInfoDao.updateById(tempDTO);
				if(count == 1){
                    UserInfo userInfo = new UserInfo();
                    userInfo.setId(tempDTO.getUserId());
                    userInfo.setBankCard(info.getBankCard());
                    userInfo.setBankCardOwner(info.getDistributorName());
                    userInfoDao.updateById(userInfo);

					map.put("err_code", 0);
					map.put("err_msg", "更新分销商记录成功");
					map.put("count", count);
					map.put("id", tempDTO.getId());
					return map;
				}
			}else{
				//为空，则为第一次升级，需要的操作就是，新增一条升级中的数据(临时数据，是失效的数据)
				//新生成分销商记录
				info.setId(null);
				info.setUserId(updateInfo.getUserId());
				info.setDistributorName(updateInfo.getDistributorName());
				info.setDistributorType("0");
				info.setDisabledFlag(1);//默认生成一条作废的临时数据 ，支付成功后才会写成有效数据
				info.setCreateTime(new Timestamp(System.currentTimeMillis()));
				info.setPostalStatus("0");
				info.setShareCode(updateInfo.getShareCode());
				info.setIsUpgrading("0");//默认正在升级中,然后由支付成功会写修改状态为1，完成升级
				info.setBankCard(updateInfo.getBankCard());
				info.setIdCard(updateInfo.getIdCard());
				count = distributorInfoDao.insert(info);
				if(count == 1){
                    UserInfo userInfo = new UserInfo();
                    userInfo.setId(tempDTO.getUserId());
                    userInfo.setBankCard(info.getBankCard());
                    userInfo.setBankCardOwner(info.getDistributorName());
                    userInfoDao.updateById(userInfo);

					map.put("err_code", 0);
					map.put("err_msg", "新增高级分销商记录成功");
					map.put("count", count); 	
					map.put("id", info.getId());
				}else{
					map.put("err_code", 101);
					map.put("err_msg", "新增高级分销商记录失败");
					map.put("count", count);
					map.put("id", null);
				}
			}
		}
		return map;
	}

	@Override
	public Object isPromoterByUserId(DistributorInfo info) {
		Map<String,Object> map = new HashMap<String,Object>();
		DistributorInfoDTO dto = distributorInfoDao.queryNeedInfoByUserId(info.getUserId());
		if(dto != null){
			if("0".equals(dto.getDistributorType())){//普通推广者时，可能是分销商队员
				DistributorTeamInfoDTO paramDTO = new DistributorTeamInfoDTO();
				paramDTO.setAuditStatus("1");
				paramDTO.setUserId(info.getUserId());
				List<DistributorTeamInfoDTO> teamInfoList = distributorTeamInfoDao.queryByUserId(paramDTO);
				if(teamInfoList !=null && teamInfoList.size() == 1){
					DistributorTeamInfoDTO teamInfo = teamInfoList.get(0);
					if("01".equals(teamInfo.getTeamType())){//中级分销商
						dto.setDistributorType("01");//自定义类型，中级分销商队员
					}else if("02".equals(teamInfo.getTeamType())){//高级分销商
						dto.setDistributorType("02");//自定义类型，高级分销商队员
					}else if("11".equals(teamInfo.getTeamType())){//讲师
						dto.setDistributorType("11");//自定义类型，讲师队员
					}else if("12".equals(teamInfo.getTeamType())){//明师
						dto.setDistributorType("12");//自定义类型，明师队员
					}else if("2".equals(teamInfo.getTeamType())){//服务站
						dto.setDistributorType("22");//自定义类型，服务站队员
						ServiceStationInfo ssInfo = serviceStationInfoDao.queryByTeamMemberUserId(info.getUserId());
						if(ssInfo != null){
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
							teamInfo.setBindTimeString(sdf.format(ssInfo.getSignTime()));
							teamInfo.setDeadlineTimeString(sdf.format(ssInfo.getDeadlineTime()));
							teamInfo.setMobilePhone(ssInfo.getAdminMobilePhone());
						}
					}else if("3".equals(teamInfo.getTeamType())){//合作方
						dto.setDistributorType("33");//自定义类型，合作方队员
						PartnerInfoDTO partnerDTO = partnerInfoDao.queryByTeamMemberUserId(info.getUserId());
						if(partnerDTO != null){
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
							teamInfo.setBindTimeString(sdf.format(partnerDTO.getSignTime()));
							teamInfo.setDeadlineTimeString(sdf.format(partnerDTO.getDeadlineTime()));
							teamInfo.setMobilePhone(partnerDTO.getMobilePhone());
						}
					}else if("4".equals(teamInfo.getTeamType())){//家人团队
						dto.setDistributorType("44");//自定义类型，家人团队
					}
					//一级的分成比例
					dto.setSharingProportion(teamInfo.getSharingProportion());
					//一级许诺给其的分成比例
					dto.setTeamSharingProportion(teamInfo.getTeamSharingProportion());
					map.put("teamInfo", teamInfo);
				}	
			}else if("3".equals(dto.getDistributorType())){
				PartnerInfoDTO partnerDTO = partnerInfoDao.queryByUserId(info.getUserId());
				if(partnerDTO != null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					dto.setUpgradeTimeString(sdf.format(partnerDTO.getSignTime()));
					dto.setDeadlineTimeString(sdf.format(partnerDTO.getDeadlineTime()));
				}
			}else if("6".equals(dto.getDistributorType())){
				ServiceStationInfo ssInfo = serviceStationInfoDao.queryByUserId(info.getUserId());
				if(ssInfo != null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					dto.setUpgradeTimeString(sdf.format(ssInfo.getSignTime()));
					dto.setDeadlineTimeString(sdf.format(ssInfo.getDeadlineTime()));
				}
			}
			map.put("err_code", 0);
			map.put("err_msg", "是推广者");
			map.put("info", dto);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "不是推广者");
			map.put("info", dto);
		}
		return map;
	}

	@Override
	public Object txCheckDistributorStatusExpired() {
		Map<String,Object> map = new HashMap<String,Object>();
		List<DistributorInfo> expireList = distributorInfoDao.queryExpireDistributor();
		if(expireList != null && expireList.size() > 0){
			for(DistributorInfo info : expireList){
				info.setDisabledFlag(1);
				int row = distributorInfoDao.updateById(info);
				if(row == 1){
					
					//查询 是否还有其他(非普通推广者且启用)身份
					//如果还有其他(非普通推广者且启用)身份, 则不作废其下属团队成员
					DistributorInfo valid = distributorInfoDao.queryValidPomotorByUserId(info.getUserId());
					if (valid == null) {
						//作废分销队员 
						List<DistributorTeamInfo> teamList = distributorTeamInfoDao.queryByBelongTeamId(info.getUserId());
						if(teamList != null && teamList.size() > 0){
							for(DistributorTeamInfo team : teamList){
								team.setDisableFlag(1);
								distributorTeamInfoDao.updateById(team);
							}
						}
					}
				}
			}
			map.put("err_code", 0);
			map.put("err_msg", "成功更新了"+ expireList.size() + "个分销商");
		}else {
			map.put("err_code", 2);
			map.put("err_msg", "没有过期的分销商更新");
		}
		return map;
	}

	@Override
	public Object txUpdateDistributors(DistributorInfo info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		if (info.getIds()==null||info.getIds().length==0) {
			map.put("err_code", 400);
			map.put("err_msg", "请勾选要操作的记录");
			return map;
		}
		int count = distributorInfoDao.updateDistributors(info);
		if(count > 0){
			DistributorInfoDTO di = null;
			for (Integer id : info.getIds()) {
				di = distributorInfoDao.queryByIdNoDisableFlag(id);
				//操作推广身份信息
				String[] distributorTypes = {"1","2","3","51","52","6","7"};
				UpdateDisableFlagUtil.updateDisableFlagForTeam(distributorTypes,
						di.getUserId(), info.getDisableFlag());
			}
			map.put("err_code", 200);
			map.put("err_msg", "操作成功,"+count+"条记录受影响");
		}else {
			map.put("err_code", 500);
			map.put("err_msg", "操作失败,"+count+"条记录受影响");
		}
		return map;
	}

	@Override
	public Object txDeleteDistributors(DistributorInfo info) {
		Map<String,Object> map = new HashMap<String,Object>();
		int count = distributorInfoDao.deleteDistributors(info);
		if(count > 0){
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
			map.put("count", count);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
			map.put("count", count);
		}
		return map;
	}

	@Override
	public Object exportExl(DistributorInfoDTO dto, HttpServletResponse response,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<DistributorInfoDTO> dataset = distributorInfoDao.queryPageByDistributorType(dto);
		if (dataset == null || dataset.size()==0) {
			map.put("err_code", 404);
			map.put("err_msg", "无可导出数据");	
			return map;
		}
		for (DistributorInfoDTO distributorInfoDTO : dataset) {
			if (distributorInfoDTO.getPostalStatus().equals("0")) {
				distributorInfoDTO.setPostalStatus("待邮寄");
			}
			if (distributorInfoDTO.getPostalStatus().equals("1")) {
				distributorInfoDTO.setPostalStatus("已邮寄");
			}
		}
		try {
			ExcelUtil.writeExcel(response, "分销商"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", dataset, DistributorInfoDTO.class);
			map.put("err_code", 200);
			map.put("err_msg", "导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "导出失败");
		}
		return map;
	}

	@Override
	public Object exportExl2(DistributorInfoDTO dto, HttpServletResponse response,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<PromoterDTO> dataset = distributorInfoDao.queryDistributorInfoPage2(dto);
		if (dataset == null || dataset.size()==0) {
			map.put("err_code", 404);
			map.put("err_msg", "无可导出数据");	
			return map;
		}
		//0=普通推广者，1=中级分销商 , 2 =高级分销商, 3=合作方, 4=过期 ,51:讲师,52:明师, 6：服务站, 7:家人
		for (PromoterDTO promoterDTO : dataset) {
			if (StringUtils.isNotBlank(promoterDTO.getDistributorType())) {
				if (promoterDTO.getDistributorType().equals("0")) {
					promoterDTO.setDistributorType("普通推广者");
				}
				if (promoterDTO.getDistributorType().equals("1")) {
					promoterDTO.setDistributorType("中级分销商");
				}
				if (promoterDTO.getDistributorType().equals("2")) {
					promoterDTO.setDistributorType("中级分销商");
				}
				if (promoterDTO.getDistributorType().equals("3")) {
					promoterDTO.setDistributorType("中级分销商");
				}
				if (promoterDTO.getDistributorType().equals("4")) {
					promoterDTO.setDistributorType("过期");
				}
				if (promoterDTO.getDistributorType().equals("51")) {
					promoterDTO.setDistributorType("讲师");
				}
				if (promoterDTO.getDistributorType().equals("52")) {
					promoterDTO.setDistributorType("明师");
				}
				if (promoterDTO.getDistributorType().equals("6")) {
					promoterDTO.setDistributorType("服务站");
				}
				if (promoterDTO.getDistributorType().equals("7")) {
					promoterDTO.setDistributorType("家人");
				}
			}
		}
		try {
			ExcelUtil.writeExcel(response, "推广者"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", dataset, PromoterDTO.class);
			map.put("err_code", 200);
			map.put("err_msg", "导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "导出失败");
		}
		return map;
	}
}


		
	